package com.hblack.manage.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hblack.manage.bean.ItemCatData;
import com.hblack.manage.bean.ItemCatResult;
import com.hblack.manage.mapper.ItemCatMapper;
import com.hblack.manage.pojo.ItemCat;

@Service
public class ItemCatService extends BaseService<ItemCat>{

	
	public ItemCatResult queryItemCatTreeAll() {
		
		//查出所有ItemCat
		List<ItemCat> list = super.queryAll();
		
		//创建集合存parentId，以及parentId值等于key的集合为value
		Map<Long, List<ItemCat>> itemCatMap = new HashMap<>();
		for (ItemCat itemCat : list) {
			if(!itemCatMap.containsKey(itemCat.getParentId())){
				//如果Map的key不包含parentId，则将其作为key，他子类集合作为value存入map
				itemCatMap.put(itemCat.getParentId(), new ArrayList<ItemCat >());
			}
			
			
			itemCatMap.get(itemCat.getParentId()).add(itemCat);
		}
		
		ItemCatResult result = new ItemCatResult();
		
		//获取parentId为0的集合
		List<ItemCat> itemCatList_1 = itemCatMap.get(0L);
		
		for (ItemCat itemCat : itemCatList_1) {
			//创建一级对象，根据itemCat赋值
			ItemCatData itemCatData_0 = new ItemCatData();
			itemCatData_0.setUrl("/products/" + itemCat.getId() + "/html");
			itemCatData_0.setNname("<a href='/products/1.html'>" + itemCat.getName() + "</a>");
			//将一级对象存入ItemCatResult中
			result.getItemCats().add(itemCatData_0);
			
			if(!itemCat.getIsParent()){
				continue;
			}
			
			//获取itemCat的主键位parentId的所有ItemCat
			List<ItemCat> itemCatList_2 = itemCatMap.get(itemCat.getId());
			//创建一级对象的items
			List<ItemCatData> itemCatData_1_list = new ArrayList<>();
			//设置一级对象的items
			itemCatData_0.setItems(itemCatData_1_list);
			
			//遍历一级对象的儿子
			for (ItemCat itemCat2 : itemCatList_2) {
				//创建二级对象
				ItemCatData itemCatData_1 = new ItemCatData();
				itemCatData_1.setUrl("/products/" + itemCat2.getId() + "/html");
				itemCatData_1.setNname(itemCat2.getName());
				//将二级对象存入一级对象的items
				itemCatData_1_list.add(itemCatData_1);
				
				if(itemCat2.getIsParent()){
					//查找所有itemCat2的主键位parentId的ItemCat
					List<ItemCat> itemCatList_3 = itemCatMap.get(itemCat2.getId());
					
					//创建二级对象的items
					List<String> itemCatData_2_list = new ArrayList<>();
					//设置二级对象的items
					itemCatData_1.setItems(itemCatData_2_list);
					
					for (ItemCat itemCat3: itemCatList_3) {
						itemCatData_2_list.add("/products/" + itemCat3.getId() + ".html|"+itemCat3.getName());
					}
				}
				
				if(result.getItemCats().size() >= 14) {
					break;
				}
			}
		}
		
		return result;
	}

	
 	
/* 	@Autowired
	private ItemCatMapper itemCatMapper;
	
	public List<ItemCat> queryItemCatsByParentId(Long parentId){
		ItemCat record = new ItemCat();
		record.setParentId(parentId);
		record.setStatus(1);
		
		return itemCatMapper.select(record);
		
	}
*/
	
}
